package com.example.downloadmvpdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements MainContract.MainView {
    // Log tag
    private static final String LOG_TAG = "MainActivity";

    // TextView
    private TextView mTextViewDownloadProgress = null;

    // ImageView
    private ImageView mImageViewDownloadStatus = null;

    // ProgressBar
    private ProgressBar mProgressBarDownloadStep = null;

    // Button
    private Button mButtonStart = null;
    private Button mButtonStop  = null;
    private Button mButtonGoon  = null;
    private Button mButtonPause = null;

    // UIHandler
    private UIHandler mUIHandler = new UIHandler();

    //
    private MainContract.Persenter persenter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DEBUG("onCreate.");

        initView();

        persenter = new PersenterImpl(this, new DownloadUtilImpl());
    }

    private void initView() {
        DEBUG("initView.");

        mTextViewDownloadProgress = findViewById(R.id.tv_show_download_progress);
        mImageViewDownloadStatus  = findViewById(R.id.iv_show_download_status);
        mProgressBarDownloadStep  = findViewById(R.id.pb_show_show_progressbar_progress);

        mButtonStart = findViewById(R.id.btn_start);
        mButtonStop  = findViewById(R.id.btn_stop);
        mButtonGoon  = findViewById(R.id.btn_goon);
        mButtonPause = findViewById(R.id.btn_pause);

        mButtonStart.setOnClickListener(new ButtonListener());
        mButtonStop.setOnClickListener(new ButtonListener());
        mButtonGoon.setOnClickListener(new ButtonListener());
        mButtonPause.setOnClickListener(new ButtonListener());

        mButtonPause.setEnabled(false);
        mButtonGoon.setEnabled(false);
    }

    private void updateTextView(int progress) {
        mTextViewDownloadProgress.setText(progress + "%");
    }

    private void updateImageView(int status) {
        switch (status) {
            case StatusMsg.IMAGE_DOWNLOAD_STATUS_INIT:
                mImageViewDownloadStatus.setImageDrawable(getDrawable(R.mipmap.download_init));
                break;
            case StatusMsg.IMAGE_DOWNLOAD_STATUS_START:
                mImageViewDownloadStatus.setImageDrawable(getDrawable(R.mipmap.download_ing));
                break;
            case StatusMsg.IMAGE_DOWNLOAD_STATUS_STOP:
                mImageViewDownloadStatus.setImageDrawable(getDrawable(R.mipmap.download_pause));
                break;
            case StatusMsg.IMAGE_DOWNLOAD_STATUS_FINISH:
                mImageViewDownloadStatus.setImageDrawable(getDrawable(R.mipmap.download_success));
                break;
                default:
                    break;
        }
    }

    private void uiInit() {
        mTextViewDownloadProgress.setText(R.string.download_progress);
        mImageViewDownloadStatus.setImageDrawable(getDrawable(R.mipmap.download_init));
        mProgressBarDownloadStep.setProgress(0);
    }

    private void uiButtonInit() {
        mButtonStart.setEnabled(true);
        mButtonPause.setEnabled(false);
        mButtonGoon.setEnabled(false);
    }

    private void updateProgressBar(int step) {
        mProgressBarDownloadStep.setProgress(step);
    }

    @Override
    public void onTextViewShowProgress(int progress) {
        Message message = new Message();
        message.what = StatusMsg.UPDATE_TEXTVIEW_DOWNLOAD_PROGRESS;
        message.arg1 = progress;
        mUIHandler.sendMessage(message);
    }

    @Override
    public void onImageViewShowDownloadStatus(int status) {
        Message message = new Message();
        message.what = StatusMsg.UPDATE_IMAGEVIEW_DOWNLOAD_STATUS;
        message.arg1 = status;
        mUIHandler.sendMessage(message);
    }

    @Override
    public void onProgressBarStep(int step) {
        Message message = new Message();
        message.what = StatusMsg.UPDATE_PROGRESSBAR_DOWNLOAD_STEP;
        message.arg1 = step;
        mUIHandler.sendMessage(message);
    }

    @Override
    public void onDownloadFinished() {
        Message message = new Message();
        message.what = StatusMsg.UI_BUTTON_INIT;
        mUIHandler.sendMessage(message);
    }


    class UIHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case StatusMsg.UI_INIT:
                    uiInit();
                    uiButtonInit();
                    break;
                case StatusMsg.UI_BUTTON_INIT:
                    uiButtonInit();
                    break;
                case StatusMsg.UPDATE_TEXTVIEW_DOWNLOAD_PROGRESS:
                    updateTextView(msg.arg1);
                    break;
                case StatusMsg.UPDATE_IMAGEVIEW_DOWNLOAD_STATUS:
                    updateImageView(msg.arg1);
                    break;
                case StatusMsg.UPDATE_PROGRESSBAR_DOWNLOAD_STEP:
                    updateProgressBar(msg.arg1);
                    break;
                    default:
                        break;
            }
        }
    }

    class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            persenter.onButtonClick(view);
            switch (view.getId()) {
                case R.id.btn_start:
                    mButtonStart.setEnabled(false);

                    mButtonPause.setEnabled(true);
                    mButtonGoon.setEnabled(false);
                    break;
                case R.id.btn_pause:
                    mButtonPause.setEnabled(false);
                    mButtonStart.setEnabled(false);
                    mButtonGoon.setEnabled(true);
                    break;
                case R.id.btn_goon:
                    mButtonPause.setEnabled(true);
                    mButtonStart.setEnabled(false);
                    mButtonGoon.setEnabled(false);
                    break;
                case R.id.btn_stop:
                    mButtonStart.setEnabled(true);
                    mButtonPause.setEnabled(false);
                    mButtonGoon.setEnabled(false);

                    Message message = new Message();
                    message.what = StatusMsg.UI_INIT;
                    mUIHandler.sendMessage(message);
                    break;
                    default:
                        break;
            }
        }
    }

    private void DEBUG(String info) {
        Log.d(LOG_TAG, info);
    }
}
